using Avalonia.Controls;
using Emgu.CV;
using System;

namespace TestEmcuAvalonia.Views
{
    public partial class MainView : UserControl
    {

        private const string video_url = "rtsp://admin:zckj888888@192.168.1.64:554/h264/ch1/sub/av_stream";
        VideoCapture capture;
        public MainView()
        {
            InitializeComponent();
            btn.Click += Btn_Click;
        }

        private void Btn_Click(object sender, Avalonia.Interactivity.RoutedEventArgs e)
        {
            capture = new VideoCapture(video_url);
            capture.ImageGrabbed += Capture_ImageGrabbed;
            capture.Grab();
            capture.Start();
        }

        int fps = 0;
        DateTime startTime = DateTime.Now;

        private void Capture_ImageGrabbed(object sender, EventArgs e)
        {
            fps++;
            Mat image = new Mat();
            capture.Retrieve(image);
            Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
            {
               var c=  image.ToBitmap();
                //picCAM.Source = image.ToBitmapSource();
            });

            TimeSpan ts = DateTime.Now - startTime;
            if (ts.TotalSeconds > 1)
            {
                startTime = DateTime.Now;
                int c = fps;
                Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(new Action(() =>
                {
                    btn.Content = c;
                }));
                fps = 0;

            }

        }
    }
}